package com.ruoyi.project.video.service.impl;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.video.mapper.VideoMapper;
import com.ruoyi.project.video.domain.Video;
import com.ruoyi.project.video.service.IVideoService;
import com.ruoyi.common.utils.text.Convert;

import static java.util.stream.Collectors.toList;

/**
 * 视频信息Service业务层处理
 * 
 * @author guozy
 * @date 2023-03-13
 */
@Service
public class VideoServiceImpl implements IVideoService 
{
    @Autowired
    private VideoMapper videoMapper;

    /**
     * 查询视频信息
     * 
     * @param title 视频信息主键
     * @return 视频信息
     */
    @Override
    public Video selectVideoByTitle(String title)
    {
        return videoMapper.selectVideoByTitle(title);
    }

    @Override
    public List<Video> selectVideoByTitleList(List<String> titleList) {
        return videoMapper.selectVideoByTitles(titleList);
    }

    /**
     * 查询视频信息列表
     * 
     * @param video 视频信息
     * @return 视频信息
     */
    @Override
    public List<Video> selectVideoList(Video video)
    {
        return videoMapper.selectVideoList(video);
    }

    /**
     * 新增视频信息
     * 
     * @param video 视频信息
     * @return 结果
     */
    @Override
    public int insertVideo(Video video)
    {
        return videoMapper.insertVideo(video);
    }

    /**
     * 修改视频信息
     * 
     * @param video 视频信息
     * @return 结果
     */
    @Override
    public int updateVideo(Video video)
    {
        return videoMapper.updateVideo(video);
    }

    /**
     * 批量删除视频信息
     * 
     * @param titles 需要删除的视频信息主键
     * @return 结果
     */
    @Override
    public int deleteVideoByTitles(String titles)
    {
        return videoMapper.deleteVideoByTitles(Convert.toStrArray(titles));
    }

    /**
     * 删除视频信息信息
     * 
     * @param title 视频信息主键
     * @return 结果
     */
    @Override
    public int deleteVideoByTitle(String title)
    {
        return videoMapper.deleteVideoByTitle(title);
    }

    @Override
    public int insertVideoList(List<Video> videoList) {
        List<String> titleList = videoList.stream().map(v -> v.getTitle()).collect(toList());
        List<Video> videoExistList = selectVideoByTitleList(titleList);
        Set<String> videoExistSet = videoExistList.stream().map(Video::getTitle).collect(Collectors.toSet());
        videoList = videoList.stream().filter(v -> !videoExistSet.contains(v.getTitle())).collect(Collectors.toList());
        int result = 0;
        for (Video video : videoList) {
            int i = videoMapper.insertVideo(video);
            result = result + i;
        }
        return result;
    }
}
